#ifndef UPLOADTHREADLIST_H
#define UPLOADTHREADLIST_H

#include <QMap>
#include <QList>
#include "PluginsInterface.h"
#include <QHttp>
#include <QMutex>
class QIODevice;

class uploadThreadList
{
private:
	struct uploadThread {
		QHttp *httpHandler;
		QList<QPair<ImageToUpload, UploadResult> > imagesAndResults;
		QIODevice *resultPage;
//		QList<ImageToUpload> images;
		QString errorString;
		int httpPostId;
		bool finished;
	};
	QList<QPair<ImageToUpload, UploadResult> > broken;
	QMap<const QHttp*, uploadThread*> httpMap;
	uploadThread* findThread(const QHttp* httpHandler);
	QMutex locker;
	uploadThread* append(QHttp* httpHandler);
	bool findResult(uploadThread* thread, const UploadResult &uploadRes, int &index);
public:
	void append(ImageToUpload image, QHttp* httpHandler, int httpPostId, bool finished = false);
	void append(ImageToUpload image, QHttp* httpHandler, bool finished = false);
	void append(QList<ImageToUpload> images, QHttp* httpHandler, int httpPostId, bool finished);
	void append(QList<ImageToUpload> images, QHttp* httpHandler, bool finished);
	void setHttpPostId(const QHttp* httpHandler, int httpPostId);
	void appendBroken(ImageToUpload image, QString error);
	QHttp::Error errorReason(const QHttp* httpHandler);
	void setFinished(const QHttp* httpHandler, const bool &finished = true);
	bool isFinished(const QHttp* httpHandler);
	bool success(const QHttp* httpHandler);
	void finishThread(const QHttp* httpHandler);
	int postId(const QHttp* httpHandler);
	QList<UploadResult> uploadedLinks(const QHttp* httpHandler);
	QList<ImageToUpload> images(const QHttp* httpHandler);
	UploadResult uploadedLinks(const QHttp* httpHandler, int imageIndex);
//	void setUploadResult(const QHttp* httpHandler, UploadResult &uploadResult);
	void mergeUploadResult(const QHttp* httpHandler, const UploadResult &uploadResult, int postIndex);
	void mergeUploadResult(const QHttp* httpHandler, const QList<UploadResult> &uploadResult);
	QIODevice *resultPage(const QHttp* httpHandler);
	void setResultPage(const QHttp* httpHandler, QIODevice* resultPage);
	void setErrorString(const QHttp* httpHandler, const QString &errorStr, const bool error = true);
	QString errorString(const QHttp* httpHandler);
	QString errorString(const QHttp* httpHandler, int imageIndex);
//	QString errorString(const int imageIndex);
	QList<int> imageIndex(const QHttp* httpHandler);
	QList<UploadResult> linksList();
	int countRunning();
	void abortAllRunning();
	void truncate();
/*private slots:
	void readRespHeaders(const QHttpResponseHeader &responseHeader);
	void httpRequestFinished(int postId, bool error);
*/};

#endif // UPLOADTHREADLIST_H
